//
//  XHBaseTableCell.m
//  Bees
//
//  Created by Xiaohan on 2018/12/3.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XHBaseTableCell.h"
#define kBaseReuseIdentifier [NSString stringWithFormat:@"%@ReuseIdentifier",NSStringFromClass(self)]
@implementation XHBaseTableCell
+ (instancetype)cellForTableView:(UITableView *)tableView{
    Class SubCellClass = [self class];
    if (SubCellClass && [SubCellClass isSubclassOfClass:XHBaseTableCell.class]) {
        id cell = [tableView dequeueReusableCellWithIdentifier:kBaseReuseIdentifier];
        if (!cell) {
            cell = [[SubCellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kBaseReuseIdentifier];
        }
        return cell;
    }
    return [XHBaseTableCell new];
}
+ (instancetype)cellForTableView:(UITableView *)tableView andCellStyle:(UITableViewCellStyle)style{
    Class SubCellClass = [self class];
    if (SubCellClass && [SubCellClass isSubclassOfClass:XHBaseTableCell.class]) {
        id cell = [tableView dequeueReusableCellWithIdentifier:kBaseReuseIdentifier];
        if (!cell) {
            cell = [[SubCellClass alloc]initWithStyle:style reuseIdentifier:kBaseReuseIdentifier];
        }
        return cell;
    }
    return [XHBaseTableCell new];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            for (UIView *v in control.subviews){
                if ([v isKindOfClass: [UIImageView class]]) {
                    UIImageView *img=(UIImageView *)v;
                    //                    img.frame = CGRectMake(img.frame.origin.x, CountHeight(20), img.frame.size.width, img.frame.size.height);
                    img.center = CGPointMake(img.center.x, img.frame.size.height+5);
                    if (!self.selected) {
                        img.image=[UIImage imageNamed:@"yuan_unselect"];
                    }
                }
            }
        }
    }
}

-(void)layoutSubviews{
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            for (UIView *v in control.subviews){
                if ([v isKindOfClass: [UIImageView class]]) {
                    UIImageView *img=(UIImageView *)v;
                    img.center = CGPointMake(img.center.x, img.frame.size.height+5);
                    //                    img.frame = CGRectMake(img.frame.origin.x, CountHeight(20), img.frame.size.width, img.frame.size.height);
                    if (self.selected) {
                        img.image=[UIImage imageNamed:@"yuan_select"];
                    }else{
                        img.image=[UIImage imageNamed:@"yuan_unselect"];
                    }
                }
            }
        }
    }
    [super layoutSubviews];
}
+ (NSString*)ReuseIdentifier{
    return kBaseReuseIdentifier;
}
@end
